package com.robot.merchant.mq;

import com.alibaba.fastjson.JSON;
import com.robot.merchant.dao.CompanyDao;
import com.robot.merchant.dao.IndustryDao;
import com.robot.merchant.dao.StoreDao;
import com.robot.merchant.pojo.Company;
import com.robot.merchant.pojo.Industry;
import com.robot.merchant.pojo.Store;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import util.GeoHashUtil;
import util.String2JsonUtil;

import java.util.Map;

@Component
@RabbitListener(queues = "merchant")
public class MerchantListener {
    @Autowired
    private StoreDao storeDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private IndustryDao industryDao;

    @RabbitHandler
    public void exec(String str){
        GeoHashUtil geoHashUtil=new GeoHashUtil();
        if(str.startsWith("store")){
            Company company=JSON.parseObject(str.replace("store",""), Company.class);
            company.setIsChain("0");
            Store store= JSON.parseObject(str.replace("store",""),Store.class);
            Map<String,String>map=(Map)JSON.parse(str.replace("store",""));
            //不要问我为什么这么写fastJson有bug，艹TM
            String name=map.get("Name");
            store.setStoreName(name);

            String latitude=store.getLatitude().trim();
            String longitude=store.getLongitude().trim();
            System.out.println("====================\n\n\n");
            System.out.println("latitude="+latitude+" longitude="+longitude);
            System.out.println("====================\n\n\n");
            boolean checkFuck= !latitude.equals("") && !longitude.equals("");
            if(checkFuck){
                String geoHash=geoHashUtil.encode(Double.parseDouble(store.getLatitude()) ,Double.parseDouble(store.getLongitude()));
                store.setGeoHash(geoHash);
                store.setHalfGeoHash(geoHashUtil.halfGeoHash(Double.parseDouble(store.getLatitude()), Double.parseDouble(store.getLongitude())));
            }

            //艹TM的那帮傻逼idcard很多都是空的mmp 如果空用id顶替一下
//            if(company.getIdCard()==null|| company.getIdCard().trim().equals("")){
//                company.setIdCard(company.getId());
//                System.out.println(company.toString());
//            }
            store.setImages(String2JsonUtil.string2Json(store.getImages()));
            companyDao.save(company);
            storeDao.save(store);
        }else if(str.startsWith("industry")){
            Industry industry=JSON.parseObject(str.replace("industry",""),Industry.class);
            industryDao.save(industry);
        }


    }

}
